/*
 * TMC4331_Constants.h
 *
 *  Created on: 23.07.2018
 *      Author: LK
 */

#ifndef TMC_IC_TMC4331_TMC4331_CONSTANTS_H_
#define TMC_IC_TMC4331_TMC4331_CONSTANTS_H_

#include "../../helpers/Constants.h"

#define TMC4331_REGISTER_COUNT   TMC_REGISTER_COUNT
#define TMC4331_MOTORS           1
#define TMC4331_WRITE_BIT        TMC_WRITE_BIT
#define TMC4331_ADDRESS_MASK     TMC_ADDRESS_MASK
#define TMC4331_MAX_VELOCITY     s32_MAX
#define TMC4331_MAX_ACCELERATION u24_MAX

#define TMC4331_COVER_DONE  (1<<25)

#define TMC4331_RAMP_HOLD      0
#define TMC4331_RAMP_TRAPEZ    1
#define TMC4331_RAMP_SSHAPE    2
#define TMC4331_RAMP_POSITION  4
#define TMC4331_RAMP_HOLD      0

// Write-Bit
#define TMC4331_WRITE 0x80

//TMC4331 GENERAL_CONFIG Bits
#define TMC4331_GCONF_USE_AVSTART                  0x00000001
#define TMC4331_GCONF_DIRECT_ACC_EN                0x00000002
#define TMC4331_GCONF_DIRECT_BOW_EN                0x00000004
#define TMC4331_GCONF_STEP_INACT_POL               0x00000008
#define TMC4331_GCONF_TOGGLE_STEP                  0x00000010
#define TMC4331_GCONF_POL_DIR_OUT                  0x00000020
#define TMC4331_GCONF_INT_SD                       0x00000000
#define TMC4331_GCONF_EXT_SD_HIGH                  0x00000040
#define TMC4331_GCONF_EXT_SD_LOW                   0x00000080
#define TMC4331_GCONF_EXT_SD_TOGGLE                0x000000C0
#define TMC4331_GCONF_DIR_IN_POL                   0x00000100
#define TMC4331_GCONF_SD_INDIRECT                  0x00000200
#define TMC4331_GCONF_ENC_INC                      0x00000000
#define TMC4331_GCONF_ENC_SSI                      0x00000400
#define TMC4331_GCONF_ENC_BISS                     0x00000800
#define TMC4331_GCONF_ENC_SPI                      0x00000C00
#define TMC4331_GCONF_ENC_DIFF_DIS                 0x00001000
#define TMC4331_GCONF_STDBY_CLOCK_LOW              0x00000000
#define TMC4331_GCONF_STDBY_CLOCK_HIGH             0x00002000
#define TMC4331_GCONF_STDBY_CHOPSYNC               0x00004000
#define TMC4331_GCONF_STDBY_CLOCK_INT              0x00006000
#define TMC4331_GCONF_INTR_POL                     0x00008000
#define TMC4331_GCONF_TARGET_REACHED_POL           0x00010000
#define TMC4331_GCONF_CLK_GATING_EN                0x00020000
#define TMC4331_GCONF_CLK_GATING_STDBY_EN          0x00040000
#define TMC4331_GCONF_FS_EN                        0x00080000
#define TMC4331_GCONF_FS_SDOUT                     0x00100000
#define TMC4331_GCONF_DCSTEP_OFF                   0x00000000
#define TMC4331_GCONF_DCSTEP_AUTO                  0x00200000
#define TMC4331_GCONF_DCSTEP_TMC21xx               0x00400000
#define TMC4331_GCONF_DCSTEP_TMC26x                0x00600000
#define TMC4331_GCONF_PWM_OUT_EN                   0x00800000
#define TMC4331_GCONF_SER_ENC_OUT_EN               0x01000000
#define TMC4331_GCONF_SER_ENC_OUT_DIFF             0x02000000
#define TMC4331_GCONF_AUTO_DIRECT_SD_OFF           0x04000000
#define TMC4331_GCONF_CIRC_CNT_XLATCH              0x08000000
#define TMC4331_GCONF_REV_DIR                      0x10000000
#define TMC4331_GCONF_INTR_TR_PU_PD_EN             0x20000000
#define TMC4331_GCONF_INTR_WIRED_AND               0x40000000
#define TMC4331_GCONF_TR_WIRED_AND                 0x80000000

//TMC4331_SPI_OUT_CONF bits
#define TMC4331_SPIOUT_OFF                         0x00000000
#define TMC4331_SPIOUT_TMC23x                      0x00000008
#define TMC4331_SPIOUT_TMC24x                      0x00000009
#define TMC4331_SPIOUT_TMC26x_389                  0x0000000A
#define TMC4331_SPIOUT_TMC26x_389_SD               0x0000000B
#define TMC4331_SPIOUT_TMC21xx_SD                  0x0000000C
#define TMC4331_SPIOUT_TMC21xx                     0x0000000D
#define TMC4331_SPIOUT_SCALE                       0x00000004
#define TMC4331_SPIOUT_SINLUT                      0x00000005
#define TMC4331_SPIOUT_DACADDR                     0x00000006
#define TMC4331_SPIOUT_DAC                         0x00000002
#define TMC4331_SPIOUT_DAC_INV                     0x00000003
#define TMC4331_SPIOUT_DAC_MAPPED                  0x00000001
#define TMC4331_SPIOUT_COVER_ONLY                  0x0000000F
#define TMC4331_SPIOUT_MD_OFF                      0x00000000
#define TMC4331_SPIOUT_MD_FALLING                  0x00000010
#define TMC4331_SPIOUT_MD_NO_STANDBY               0x00000020
#define TMC4331_SPIOUT_MD_ALWAYS                   0x00000030
#define TMC4331_SPIOUT_STDBY_ON_STALL              0x00000040
#define TMC4331_SPIOUT_STALL_FLAG                  0x00000080
#define TMC4331_STALL_LOAD_LIMIT(x)                ((x & 0x07) << 8)
#define TMC4331_SPIOUT_PHASE_SHIFT                 0x00000100
#define TMC4331_SPIOUT_THREE_PHASE_EN              0x00000010
#define TMC4331_SPIOUT_SCALE_VAL_TR_EN             0x00000020
#define TMC4331_SPIOUT_DISABLE_POLLING             0x00000040
#define TMC4331_SPIOUT_ENABLE_SHADOW_DATAGRAMS     0x00000080
#define TMC4331_SPIOUT_POLL_BLOCK_MULTI(x)         (((x) & 0x0F) << 8)
#define TMC4331_SPIOUT_COVER_DONE_NOT_FOR_CURRENT  0x00001000

//TMC4331 ENC_IN configuration bits
#define TMC4331_ENC_IN_CONF               0x00000007
#define TMC4331_ENC_IN_MODE_OL            0x00000000
#define TMC4331_ENC_IN_MODE_CL            0x00400000
#define TMC4331_ENC_IN_MODE_PID_0         0x00800000
#define TMC4331_ENC_IN_MODE PID_V         0x00C00000
#define TMC4331_ENC_IN_CL_CALIBRATION_EN  0x01000000
#define TMC4331_ENC_IN_CL_EMF_EN          0x02000000
#define TMC4331_ENC_IN_CL_VLIMIT_EN       0x08000000
#define TMC4331_ENC_IN_CL_VELOCITY_EN     0x10000000
#define TMC4331_ENC_IN_SER_VAR_LIMIT      0x80000000

//TMC4331_CURRENT_CONF bits
#define TMC4331_CURCONF_HOLD_EN          0x00000001
#define TMC4331_CURCONF_DRIVE_EN         0x00000002
#define TMC4331_CURCONF_BOOST_ACC_EN     0x00000004
#define TMC4331_CURCONF_BOOST_DEC_EN     0x00000008
#define TMC4331_CURCONF_BOOST_START_EN   0x00000010
#define TMC4331_CURCONF_SEC_DRIVE_EN     0x00000020
#define TMC4331_CURCONF_FREEWHEELING_EN  0x00000040
#define TMC4331_CURCONF_CL_SCALE_EN      0x00000080
#define TMC4331_CURCONF_PWM_SCALE_REF    0x00000100
#define TMC4331_CURCONF_PWM_AMPL(x)      (x<<16)

//TMC4331_EVENTS register bits
#define TMC4331_EV_TARGET_REACHED     0x00000001
#define TMC4331_EV_POSCOMP_REACHED    0x00000002
#define TMC4331_EV_VELOCITY_REACHED   0x00000004
#define TMC4331_EV_VZERO              0x00000008
#define TMC4331_EV_VPOSITIVE          0x00000010
#define TMC4331_EV_VNEGATIVE          0x00000020
#define TMC4331_EV_AZERO              0x00000040
#define TMC4331_EV_APOSITIVE          0x00000080
#define TMC4331_EV_ANEGATIVE          0x00000100
#define TMC4331_EV_MAX_PHASE_TRAP     0x00000200
#define TMC4331_EV_FROZEN             0x00000400
#define TMC4331_EV_STOP_LEFT          0x00000800
#define TMC4331_EV_STOP_RIGHT         0x00001000
#define TMC4331_EV_VIRT_STOP_LEFT     0x00002000
#define TMC4331_EV_VIRT_STOP_RIGHT    0x00004000
#define TMC4331_EV_HOME_ERROR         0x00008000
#define TMC4331_EV_XLATCH_DONE        0x00010000
#define TMC4331_EV_FS_ACTIVE          0x00020000
#define TMC4331_EV_ENC_FAIL           0x00040000
#define TMC4331_EV_N_ACTIVE           0x00080000
#define TMC4331_EV_ENC_DONE           0x00100000
#define TMC4331_EV_SER_ENC_DATA_FAIL  0x00200000
#define TMC4331_EV_CRC_FAIL           0x00400000
#define TMC4331_EV_SER_DATA_DONE      0x00800000
#define TMC4331_EV_BISS_FLAG          0x01000000
#define TMC4331_EV_COVER_DONE         0x02000000
#define TMC4331_EV_ENC_VZERO          0x04000000
#define TMC4331_EV_CL_MAX             0x08000000
#define TMC4331_EV_CL_FIT             0x10000000
#define TMC4331_EV_STOP_ON_STALL      0x20000000
#define TMC4331_EV_MOTOR              0x40000000
#define TMC4331_EV_RST                0x80000000

//TMC4331_STATUS register bits
#define TMC4331_ST_TARGET_REACHED          0x00000001
#define TMC4331_ST_POSCOMP_REACHED         0x00000002
#define TMC4331_ST_VEL_REACHED             0x00000004
#define TMC4331_ST_VEL_POS                 0x00000008
#define TMC4331_ST_VEL_NEG                 0x00000010
#define TMC4331_ST_RAMP_ACC                0x00000020
#define TMC4331_ST_RAMP_DEC                0x00000040
#define TMC4331_ST_STOP_LEFT_ACTIVE        0x00000080
#define TMC4331_ST_STOP_RIGHT_ACTIVE       0x00000100
#define TMC4331_ST_VIRT_STOP_LEFT_ACTIVE   0x00000200
#define TMC4331_ST_VIRT_STOP_RIGHT_ACTIVE  0x00000400
#define TMC4331_ST_HOME_ERROR              0x00001000
#define TMC4331_ST_ENC_FAIL                0x00004000

//TMC4331 ramp modes
#define TMC4331_RAMPMODE_VEL_HOLD    0
#define TMC4331_RAMPMODE_VEL_TRAPEZ  1
#define TMC4331_RAMPMODE_VEL_SSHAPE  2
#define TMC4331_RAMPMODE_POS_HOLD    4
#define TMC4331_RAMPMODE_POS_TRAPEZ  5
#define TMC4331_RAMPMODE_POS_SSHAPE  6

//TMC4331 reference switch configuration
#define TMC4331_REFCONF_STOP_LEFT_EN       0x00000001
#define TMC4331_REFCONF_STOP_RIGHT_EN      0x00000002
#define TMC4331_REFCONF_POL_STOP_LEFT      0x00000004
#define TMC4331_REFCONF_POL_STOP_RIGHT     0x00000008
#define TMC4331_REFCONF_INV_STOP_DIR       0x00000010
#define TMC4331_REFCONF_SOFT_STOP_EN       0x00000020
#define TMC4331_REFCONF_VIRT_LEFT_LIM_EN   0x00000040
#define TMC4331_REFCONF_VIRT_RIGHT_LIM_EN  0x00000080
#define TMC4331_REFCONF_VIRT_STOP_HARD     0x00000100
#define TMC4331_REFCONF_VIRT_STOP_LINEAR   0x00000200
#define TMC4331_REFCONF_CIRCULAR           0x00400000
#define TMC4331_REFCONF_STOP_ON_STALL      0x04000000
#define TMC4331_REFCONF_DRV_AFTER_STALL    0x08000000
#define TMC4331_REFCONF_CIRCULAR_ENC_EN    0x80000000

#endif /* TMC_IC_TMC4331_TMC4331_CONSTANTS_H_ */
